# -*- coding: cp1250 -*-

from imaplib import *
import string
import getpass

def getImap(server='',username='', password='', imapqry=''):
    server=IMAP4(server)
    server.login(username, password)
    r = server.select("Inbox")
    res = ''
    try:
        #for iqk in imapqueries.keys():
        r, data = server.search(None, imapqry)
        data = string.split(data[0], ' ')
        for x in data:
            #print x
            #r, data = server.fetch(str(x), '(UID BODY[TEXT])')
            #r, data = server.fetch(str(x), '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
            #r, data = server.fetch(str(x), '(INTERNALDATE)')
            r, data = server.fetch(str(x), '(UID)')
            print data
            r, data = server.fetch(str(x), '(BODY[HEADER.FIELDS (FROM)])')
            print data[0][1:][0]
            """
            for i in data[:]:
                for x in i[:]:
                    res = res + x
            """
    except:
        res = 0
    return res

def main():
    username = 'atrema\\vpresovsky'
    print username
    #password = getpass.getpass('Enter Your password to IMAP server: ')
    password = ''
    #print password, username
    print getImap(server='hubud0410.atrema.deloitte.com',
            username=username,
            password=password,
            imapqry="(SUBJECT \".ares\")")
    #imapqry="(SUBJECT \"!ares\")"

if __name__ == "__main__":
    print "non-modular use; sending test mail ..."
    main()
